package com.dineroquegastar.urule.service;

import com.bstek.urule.Utils;
import com.dineroquegastar.urule.accident.entity.Customer;
import com.bstek.urule.runtime.KnowledgePackage;
import com.bstek.urule.runtime.KnowledgeSession;
import com.bstek.urule.runtime.KnowledgeSessionFactory;
import com.bstek.urule.runtime.service.KnowledgeService;
import com.dineroquegastar.urule.entity.feature.product.FeaturesProductGradeRequest;
import com.dineroquegastar.urule.utils.jackson.JsonJacksonUtil;
import com.dineroquegastar.urule.vo.MXFirstReviewFlowResult;
import com.dineroquegastar.urule.vo.MXProductGradeResult;
import com.dineroquegastar.urule.vo.QueryLoanResultLevelParamVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 客户贷款级别决策服务
 *
 * @author liuhai
 * @date 2021/3/1 7:39 PM
 */
@Service
@Slf4j
public class LoanResultLevelService {

    /**
     * 决策客户贷款级别
     *
     * @param paramVO
     * @return
     */
    public MXProductGradeResult loanResultLevel(FeaturesProductGradeRequest paramVO) {

        KnowledgeService knowledgeService = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
        KnowledgePackage kp = null;
        //临时处理异常，待封装
        try {
            kp = knowledgeService.getKnowledge("demo/MX0001");//调用知识包MX0001
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
        /*单次调用*/
        KnowledgeSession kSession = KnowledgeSessionFactory.newKnowledgeSession(kp);
        /*传入参数 只有将参数传入决策引擎配置才可以生效*/
        kSession.insert(paramVO);

        log.info("decision param : {}",JsonJacksonUtil.toJson(paramVO));

        /*执行指定的决策流*/
        kSession.startProcess("credit_flow");

        /*获取执行结果*/
        Map<String, Object> parameter = kSession.getParameters();
        log.info(" customerId：{} get product grade parameter result：{}",paramVO.getCustomerId(), JsonJacksonUtil.toJson(parameter));

        //组织结果
        return generateMXFirstReviewFlowResult(parameter);
    }

    private MXProductGradeResult generateMXFirstReviewFlowResult(Map<String, Object> parameter) {
        List<Object> list = new ArrayList<>();

        //----------------------------------墨西哥初审决策流结果----------------------------------------
        MXProductGradeResult flowResult = new MXProductGradeResult();

        Map<String, Object> resultMap = (Map<String, Object>) parameter.get("RESULT");
        //是否调用下一流程
        String creditAmount = resultMap.get("credit_amount") == null ? null : resultMap.get("credit_amount").toString();
        flowResult.setCreditAmount(creditAmount);

        //批核结果
        String creditProductNo = resultMap.get("credit_product_no") == null ? null : resultMap.get("credit_product_no").toString();
        flowResult.setCreditProductNo(creditProductNo);

        //批核结果
        String creditAmountLevel = resultMap.get("credit_amount_level") == null ? null : resultMap.get("credit_amount_level").toString();
        flowResult.setCreditAmountLevel(creditAmountLevel);

        //批核结果
        String creditProductFee = resultMap.get("credit_product_fee") == null ? null : resultMap.get("credit_product_fee").toString();
        flowResult.setCreditProductFee(creditProductFee);

        //用户等级
        Integer userLevel = resultMap.get("user_level") == null ? null : Integer.parseInt(resultMap.get("user_level").toString());
        flowResult.setUserLevel(userLevel);

        //触碰的直拒规则
        String ruleLabel = resultMap.get("rule_label") == null ? null : resultMap.get("user_level").toString();
        flowResult.setRuleLabel(ruleLabel);

        flowResult.setDecisionName("loan_result_level_flow");

        return flowResult;
    }

}
